package com.wlgc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlgc.dao.UserDao;
import com.wlgc.entity.RestBean;
import com.wlgc.entity.User;
import com.wlgc.mapper.AssignmentMapper;
import com.wlgc.service.AssignmentService;
import com.wlgc.service.impl.CourseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/data/request")
public class DataController {
    @Resource
    AssignmentMapper mapper;

    @Resource
    AssignmentService assignmentService;

    @Value("${student_work.imgPath}")
    private String originPath;

    @Autowired
    private UserDao userDao;
    @Resource
    CourseServiceImpl courseService;

    String IllegalFiles = ".exe .js .bat .mp3 .mp4";

    String legalFiles = ".png .jpg .bmp .doc .docx .pdf";


    @GetMapping("/index")
    @PreAuthorize("hasAuthority('test')")
    public RestBean info() {
        SecurityContext context = SecurityContextHolder.getContext();
        return new RestBean(200, context.getAuthentication().getName());
    }

   //获取个人作业完成情况
    @PostMapping("/get-assignment")
    @PreAuthorize("hasAuthority('test')")
    RestBean getAssignment(@RequestParam("refer") String refer)
    {

        return assignmentService.selectAssignment(Integer.parseInt(refer));
    }


    //获取班级课程作业完成情况
    @PostMapping("/get-course-condition")
    @PreAuthorize("hasAuthority('admin')")
    RestBean getClassAssignment(@RequestParam("refer") String refer,@RequestParam("cno")String cno,@RequestParam("courseName") String courseName)
    {  refer=assignmentService.translate(refer);
        String cid=courseService.getCid(courseName);
        return assignmentService.getClass_Course_Condition(Integer.parseInt(refer),Integer.parseInt(cno),Integer.parseInt(cid));
    }

    @PostMapping("/upload")
    @PreAuthorize("hasAuthority('test')")
    public RestBean upload(String aid, String courseName, String assignmentName, MultipartFile file){
        log.info("上传图片");


        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUsername,username);
        User user = userDao.selectOne(userLambdaQueryWrapper);


        //获取文件初始名
        String originalName  = file.getOriginalFilename();
        //获取文件后缀
        String suffix = originalName.substring(originalName.lastIndexOf("."));

        log.info("文件格式{}",suffix);
        if(!legalFiles.contains(suffix)){
            return new RestBean(500,"非法文件格式，请上传.png .jpg .bmp .doc .docx .pdf格式文件");
        }

        String detail = mapper.getDetail(Integer.parseInt(aid));


        //生成新的文件名
        String fileName =  user.getId() + "-" + user.getName()+ "-" + assignmentName+ "-" + detail  + suffix;
        log.info("{}",fileName);

        String basePath =  originPath;

        //创建课程文件夹
        basePath += user.getCno() + File.separator + courseName + File.separator;
        log.info("课程路径:{}",basePath);
        File dir1 = new File(basePath);
        if(!dir1.exists()){
            dir1.mkdir();
        }

        //创建学生文件夹
        basePath += user.getId() + "-" + user.getName() + File.separator;

        File dir2 = new File(basePath);
        if(!dir2.exists()){
            dir2.mkdir();
        }

        try {
            file.transferTo(new File(basePath + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
         mapper.finishAssignment(user.getId().intValue(),Integer.parseInt(aid));
        return new RestBean(200,"上传成功");
    }

    //查询课程
 @PostMapping("/getCourse")
 @PreAuthorize("hasAuthority('test')")
    RestBean getCourse()//后面分方向需要改进/*@RequestParam("cno")String cno*/
 {
     return courseService.getAllCourse();
 }
}
